package com.jsnkj.basic.fragment

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.jsnkj.basic.R
import com.jsnkj.basic.adapter.IncomeAdapter
import com.jsnkj.basic.bean.IncomeExpenditureEntity
import com.jsnkj.basic.databinding.FragmentIncomeBinding
import com.jsnkj.basic.mvvm.BaseMVVMActivity
import com.jsnkj.basic.mvvm.BaseMVVMFragment
import com.jsnkj.basic.util.ErrorResultUtil

/**
 * 收入界面
 */
class IncomeFragment : BaseMVVMFragment<FragmentIncomeBinding, IncomeViewModel>() {

    companion object {
        fun newInstance() = IncomeFragment()
    }

    override val layoutId: Int
        get() = R.layout.fragment_income

    var incomeAdapter: IncomeAdapter? = null

    override fun initView(savedInstanceState: Bundle?) {
        mVM.errorResult.observe(this) {
            ErrorResultUtil.errorResult(requireActivity() as BaseMVVMActivity<*, *>,it)

            mVDB.rvIncome.visibility = View.GONE
            mVDB.tvError.visibility = View.VISIBLE
        }

        mVM.postHttpResult.observe(this) {
            initRecyclerView(it.list)
        }

        mVM.postWalletIncomes()
    }

    private fun initRecyclerView(list: List<IncomeExpenditureEntity>?) {
        if (list.isNullOrEmpty()) {
            return
        }

        if (null == incomeAdapter) {
            incomeAdapter = IncomeAdapter(requireActivity(), list)
        }
        mVDB.rvIncome.apply {
            layoutManager =
                LinearLayoutManager(requireActivity(), LinearLayoutManager.VERTICAL, false)
            adapter = incomeAdapter
        }

        mVDB.rvIncome.visibility = View.VISIBLE
        mVDB.tvError.visibility = View.GONE
    }
}